package team.tunan.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import team.tunan.entity.Vote;
import team.tunan.mapper.ScoreMapper;
import team.tunan.mapper.VoteMapper;
import team.tunan.service.IVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements IVoteService {
    @Autowired
    private VoteMapper voteMapper;

    @Autowired
    private ScoreMapper scoreMapper;


    @Override
    public Map<String, Object> doVote(Vote vote) {

        Map<String, Object> map = new HashMap<>();

        Integer mid = vote.getMid();
        Integer pid = vote.getPid();
        Integer uid = vote.getUid();

        // 查询用户是否投票
        List<Integer> uu = voteMapper.selectVoteToUser(mid);
        if (uu.contains(uid)) {
            // 已投票 状态1
            map.put("status", 1);
            map.put("flag", false);
            return map;
        }

        // 未投票 状态0

        // 执行投票 vote 表 新增一条数据
        Integer integer = voteMapper.addVote(vote);


        // 获取该歌手 该场比赛票数
        Integer votes = scoreMapper.getVotesByPidAndMid(pid, mid);
        votes = votes + 1;

        // 给该歌手票数 +1
        Integer integer1 = scoreMapper.updateVotesByMidAndPlayerId(votes, mid, pid);

        map.put("status", 0);
        map.put("flag", true);

        return map;
    }
}
